<?php

class AttachModel extends Model {
	protected $tableName = 'x_attach';

	/**
	 * 获取附件列表
	 * @access public
	 * @param $post Array  : 传入$_POST参数 [ userId, key, attach_type, extension, field+order, limit ]
	 */
	public function getAttachList($post) {
		$order = 'id DESC';
		$limit = '20';
		$map   = array();
		$field = 'id,uid,attach_type,name,private,size,extension,mtime,totaldowns';

		//根据POST，组装查询条件
		if(!empty($post['userId'])) {
			$map['uid'] = array('in', $post['userId']);
		}
		if(!empty($post['key'])) {
			$map['name'] = array('like', $post['key']);
		}
		if(!empty($post['attach_type'])) {
			$map['attach_type'] = $post['attach_type'];
		}
		if(!empty($post['extension'])) {
			$map['extension'] = $post['extension'];
		}
		if(!empty($post['field']) && !empty($post['order'])) {
			$order = $post['field'] . ' ' . $post['order'];
		}
		if(!empty($post['limit'])) {
			$limit = intval($post['limit']);
		}
		$map['del'] = 0;
		
		return $this->where($map)->field($field)->order($order)->findPage($limit);
	}

	//获取附件的应用类型
	public function getAttachType() {
		return formatArray( $this->field('attach_type')->Distinct(true)->findAll(), 'attach_type' );
	}

	//获取附件的扩展名
	public function getExtensionType() {
		return formatArray( $this->field('extension')->Distinct(true)->findAll(), 'extension' );
	}
}

?>